<?php

/**
 * Authentication component
 *
 * Manages user logins and permissions.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake.libs.controller.components
 * @since         CakePHP(tm) v 0.10.0.1076
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class UploadComponent extends Component {

    public $upload_path;
    private $error = array();
    private $rules = array();

    public function __construct() {

    }

    public function getExt($filename) {
        $ext = substr(strstr($filename, '.'), 1);
        return $ext;
    }

    public function validate($tmp_file) {
        //run rule about size
        $upload_fail = false;
        if (isset($this->rules['size'])) {
            $size = round($tmp_file['size'] / 1024);
            if ($size > $this->rules['size']['max'] || $size < $this->rules['size']['min']) {
                $this->setError("Kích thước file file lớn hơn {$this->rules['size']['min']}Kb và nhỏ hơn  {$this->rules['size']['max']}Kb");
                $upload_fail = true;
            }
        }
        //run rule about extension file
        if (isset($this->rules['ext'])) {
            $ext = $this->getExt($tmp_file['name']);
            if (array_search(strtolower($ext), $this->rules['ext']) === false) {
                $this->setError("Extension của file ko hợp lệ");
                $upload_fail = true;
            }
        }
        if ($upload_fail) {
            return false;
        } else {
            return true;
        }
    }

    public function setError($msg) {
        $this->error[] = $msg;
    }

    public function getError() {
        return $this->error;
    }

    public function setRules($rule_name, $rules = array()) {
        $this->rules[$rule_name] = $rules;
    }

    public function setUploadpath($pathname) {
        if (is_readable($pathname)) {
            $this->upload_path = $pathname;
        } else {
            $this->setError("path: $pathname can not write");
            return false;
        }
    }

    public function delete($filename) {
        @unlink($filename);
    }

    public function upload($tmp_file, $random_filename = true, $validate = false) {
        if ($validate) {
            $check_upload = $this->validate($tmp_file);
            if (!$check_upload) {
                return false;
            }
        }
        if ($random_filename) {
            $ext = $this->getExt($tmp_file['name']);
            $uploaded_filename = md5(microtime()) . ".$ext";
        } else {
            $uploaded_filename = $tmp_file['name'];
        }
        if ($tmp_file["error"] > 0) {
            return false;
        } else {
            if (move_uploaded_file($tmp_file['tmp_name'], $this->upload_path . $uploaded_filename)) {
                return $uploaded_filename;
            } else {
                return false;
            }
        }
    }

    function createFolder() {

        $folder = array('123', '456', '789');
        $lv1 = $folder[array_rand($folder, 1)];
        $lv2 = $folder[array_rand($folder, 1)];
        return $lv1 . DIRECTORY_SEPARATOR . $lv2 . DIRECTORY_SEPARATOR;
    }

}

